package com.zejson.controller;

import com.zejson.entity.Truck;
import com.zejson.service.TruckService;
import com.zejson.service.TruckTeamService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zejson.util.DateConvert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@RequestMapping("truck")
public class TruckController {

    @RequestMapping("")
    public ModelAndView index(HttpServletRequest request) throws ClassNotFoundException {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckService truckService = (TruckService) applicationContext.getBean("truckService");
        List<Object> trucks = truckService.getList();

        TruckTeamService truckTeamService =  ((TruckTeamService)applicationContext.getBean("truckTeamService"));
        List teams = truckTeamService.getList();

        ModelAndView model = new ModelAndView("/templates/driver/truck");
        model.addObject("trucks", trucks);
        model.addObject("teams",teams);
        return model;
    }

    @RequestMapping("add_truck")
    public Object add_truck(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, ParseException {
        if (request.getMethod().equals("POST")) {
            String number = request.getParameter("number");
            String buydate = request.getParameter("buydate");
            String type = request.getParameter("type");
            String length = request.getParameter("length");
            String tonnage = request.getParameter("tonnage");
            String fkTeamid = request.getParameter("fkTeamid");
            String remark = request.getParameter("remark");

            Truck truck = new Truck();
            truck.setNumber(number);
            truck.setBuydate(DateConvert.parseDate(buydate));
            truck.setType(Integer.parseInt(type));
            truck.setLength((float) Integer.parseInt(length));
            truck.setTonnage((float) Integer.parseInt(tonnage));
            if (!fkTeamid.equals("0"))
                truck.setFkTeamid(Integer.parseInt(fkTeamid));
            truck.setRemark(remark);
            truck.setState((byte) 0);
            truck.setIsdeleted((byte) 0);
            Date utilDate  =new Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            truck.setCheckintime(sqlDate);

            System.out.println(truck);

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            TruckService truckService = ((TruckService)applicationContext.getBean("truckService"));
            truckService.insertByAutoGeneratePK("TruckMapper",truck);
        }
        return "redirect:/truck";
    }

    @RequestMapping("update_truck")
    public String updateTruck(HttpServletRequest request) throws ClassNotFoundException, ParseException {

        Integer truckId = Integer.parseInt(request.getParameter("truck-id"));
        String number = request.getParameter("number");
        String buydate = request.getParameter("buydate");
        String type = request.getParameter("type");
        String length = request.getParameter("length");
        String tonnage = request.getParameter("tonnage");
        String fkTeamid = request.getParameter("fkTeamid");
        String remark = request.getParameter("remark");

        Truck truck = new Truck();
        truck.setNumber(number);
        truck.setBuydate(DateConvert.parseDate(buydate));
        truck.setType(Integer.parseInt(type));
        truck.setLength((float) Integer.parseInt(length));
        truck.setTonnage((float) Integer.parseInt(tonnage));
        if (!fkTeamid.equals("0"))
            truck.setFkTeamid(Integer.parseInt(fkTeamid));
        truck.setRemark(remark);
        truck.setState((byte) 0);
        truck.setIsdeleted((byte) 0);
        Date utilDate  =new Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        truck.setCheckintime(sqlDate);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckService truckService = ((TruckService)applicationContext.getBean("truckService"));
        truckService.updateById("TruckMapper",truck);

        return "redirect:/truck";
    }

    @RequestMapping("delete_truck")
    public String deleteTruck(HttpServletRequest request) throws ClassNotFoundException {

        Integer truckId = Integer.parseInt(request.getParameter("truckId"));
        System.out.println(truckId);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckService truckService = ((TruckService)applicationContext.getBean("truckService"));
        truckService.deleteById("TruckMapper", truckId);
        return "redirect:/truck";
    }


}